package io.nutz.nutzsite.module.signin.controller;

import io.nutz.nutzsite.common.base.Result;
import io.nutz.nutzsite.common.utils.DateUtils;
import io.nutz.nutzsite.common.utils.IpUtils;
import io.nutz.nutzsite.common.utils.ShiroUtils;
import io.nutz.nutzsite.module.signin.models.Sned;
import io.nutz.nutzsite.module.signin.models.Snpub;
import io.nutz.nutzsite.module.signin.services.SnedService;
import io.nutz.nutzsite.module.signin.services.SnpubService;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;
/**
 * 签到 信息操作处理
 * 
 * @author lrw
 * @date 2020-02-29
 */
@IocBean
@At("/signin/sned")
public class SnedController {
	private static final Log log = Logs.get();

	@Inject
	private SnedService snedService;
	@Inject
	private SnpubService snpubService;
	
	//@RequiresPermissions("signin:sned:view")
	@At("/detail")
	@Ok("th:/signin/sned/sned.html")
	public void index(@Param("snid")String snid,HttpServletRequest req) {
		req.setAttribute("snid", snid);
	}


	/**
	 * 查询签到列表
	 */
	//@RequiresPermissions("signin:sned:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("snid")String snid,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		cnd.and("snid", "=", snid);
		return snedService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}
	//查看属于我自己的签到任务
	@At
	@Ok("json")
	public Object list2(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		cnd.and("uid", "=", ShiroUtils.getSysUserId());
		return snedService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,"snpub");
	}
	/**
	 * 新增签到
	 */
	@At("/add")
	@Ok("th:/signin/sned/add.html")
	public void add( HttpServletRequest req) {

	}

	/**
	 * 新增保存签到
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("signin:sned:add")
	@Slog(tag="签到", after="新增保存签到 id=${args[0].id}")
	public Object addDo(@Param("..") Sned sned,HttpServletRequest req) {
		try {

			snedService.insert(sned);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 修改签到
	 */
	@At("/edit/?")
	@Ok("th://signin/sned/edit.html")
	public void edit(String id, HttpServletRequest req) {
		Sned sned = snedService.fetch(id);
		req.setAttribute("sned",sned);
	}
	/**
	 * 修改签到
	 */
	@At("/edit2/?")
	@Ok("json")
	@Slog(tag="签到", after="修改保存签到")
	public Object edit2(String snid, HttpServletRequest req) {
		
		try {
			
			Sned sned = snedService.fetch(Cnd.where("snid", "=", snid).and("uid", "=", ShiroUtils.getSysUserId()));
			sned.setSnip(IpUtils.getIpAddr(req));
			sned.setSntime(new Date());
			Snpub snpub=snpubService.fetch(Cnd.where("id", "=", snid));
			
			//Date t1=snpub.getSnstarttime();
			if(snpub.isHasendtime()) {
				Date t2=snpub.getSnendtime();
				Date t0=new Date();
				if(t2.getTime()<t0.getTime()){//若迟到，记录迟到分钟数
					int d=(int)DateUtils.getDistanceOfTwoDate(t2, t0, "m");
					sned.setStatus(d);
					snedService.update(sned);
					/*if(!snpub.isHasobj()){//指定签到人
						snpubService.update(Chain.makeSpecial("latesn", "+1").addSpecial("absentsn", "-1"),cnd);
					}else{//未指定签到人
						snpubService.update(Chain.makeSpecial("latesn", "+1"),cnd);
					}*/
				}else {
					sned.setStatus(0);//签到进行中
					snedService.update(sned);
					/*if(!snpub.isHasobj()){//指定签到人
						snpubService.update(Chain.makeSpecial("ontimesn", "+1").addSpecial("absentsn", "-1"),cnd);
					}else{//未指定签到人
						snpubService.update(Chain.makeSpecial("ontimesn", "+1"),cnd);
					}*/
				}
				
			}else {
				sned.setStatus(0);//手工没结束,签到进行中
				snedService.update(sned);
				/*if(!snpub.isHasobj()){//指定签到人
					snpubService.update(Chain.makeSpecial("ontimesn", "+1").addSpecial("absentsn", "-1"),cnd);
				}else{//未指定签到人
					snpubService.update(Chain.makeSpecial("ontimesn", "+1"),cnd);
				}*/
			}
			int absentsn=snedService.count(Cnd.where("snid", "=", snid).and("status", "=", -1));
			int ontimesn=snedService.count(Cnd.where("snid", "=", snid).and("status", "=", 0));
			int latesn=snedService.count(Cnd.where("snid", "=", snid).and("status", ">", 0));
			snpubService.update(Chain.make("ontimesn", ontimesn).add("latesn", latesn).add("absentsn", absentsn),Cnd.where("id", "=", snid));

			return Result.success("system.success");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("system.error");
		}
	}
	/**
	 * 修改保存签到
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("signin:sned:edit")
	@Slog(tag="签到", after="修改保存签到")
	public Object editDo(@Param("..") Sned sned,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(sned)){
				snedService.update(sned);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除签到
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("signin:sned:remove")
	@Slog(tag ="签到", after= "删除签到:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			snedService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
